package com.hao.crm.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hao.crm.domain.Role;
import com.hao.crm.page.AjaxResult;
import com.hao.crm.page.PageResult;
import com.hao.crm.query.QueryObject;
import com.hao.crm.service.IRoleService;
@Controller
public class RoleController {
	@Autowired
	private IRoleService roleService;
	@RequestMapping("/role")
	public String index() {
		return "role";
	}
	@RequestMapping("/role_list")
	@ResponseBody
	public PageResult list(QueryObject qo) {
		return roleService.selectByCondition(qo);
	}
	@RequestMapping("/role_save")
	@ResponseBody
	public AjaxResult save(Role role) {
		AjaxResult result = null;
		try {
			int effectRows = roleService.insert(role);
			if(effectRows>0) {
				result = new AjaxResult(true, "保存成功");
			}else {
				result = new AjaxResult("保存失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = new AjaxResult("保存异常，请联系管理员");
		}
		
		return result;
	}
	@RequestMapping("/role_update")
	@ResponseBody
	public AjaxResult update(Role role) {
		AjaxResult result = null;
		try {
			int effectRows = roleService.update(role);
			if(effectRows>0) {
				result = new AjaxResult(true, "修改成功");
			}else {
				result = new AjaxResult("修改失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = new AjaxResult("修改异常，请联系管理员");
		}
		
		return result;
	}
	@RequestMapping("/role_selectAll")
	@ResponseBody
	public List<Role> selectAll(){
		List<Role> result = roleService.selectAll();
		return result;
	}
	@RequestMapping("/role_selectByEid")
	@ResponseBody
	public List<Long> selectByEid(Long id){
		List<Long> result = roleService.selectByEid(id);
		return result;
	}
}
